home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / Rotate Bitmap 90° / Source / Rotate.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  3.7 KB  |  147 lines  |  [TEXT/CWIE]

  1. #include <Dialogs.h>
  2. #include <Fonts.h>
  3. #include <Processes.h>
  4.  
  5. #define    BIT00       0x01
  6. #define    BIT01       0x02
  7. #define    BIT02       0x04
  8. #define    BIT03       0x08
  9. #define    BIT04       0x10
  10. #define    BIT05       0x20
  11. #define    BIT06       0x40
  12. #define    BIT07       0x80
  13.  
  14. void rotate();
  15.  
  16. BitMap        *macBitMap;
  17.  
  18. void rotate()
  19. {
  20.     Ptr                srcPtr, destPtr, destColPtr, myBaseAddr;
  21.     BitMap            *newBitmap;
  22.     short            destRowBytes, bitchar, srcByte, srcH, srcV, srccols, srcrows, i;
  23.     Rect            destRect;
  24.  
  25.     /* rotate boundary rectangle */
  26.     srcH = macBitMap->bounds.right - macBitMap->bounds.left;
  27.     srcV = macBitMap->bounds.bottom - macBitMap->bounds.top;
  28.     SetRect(&destRect, macBitMap->bounds.left, macBitMap->bounds.top,
  29.             macBitMap->bounds.left+srcV, macBitMap->bounds.top+srcH);
  30.  
  31.     /* allocate destination buffer */
  32.     destRowBytes  = (srcV+7) >> 3;
  33.     newBitmap = (BitMap *) NewPtrClear(sizeof(BitMap));
  34.     myBaseAddr = (Ptr) NewPtrClear(destRowBytes*srcH);
  35.  
  36.     /* set-up src to rotated destination scan */
  37.     srccols = macBitMap->rowBytes;
  38.     srcrows = srcV;
  39.     srcPtr = macBitMap->baseAddr;
  40.     bitchar = BIT00;
  41.     destColPtr= myBaseAddr + destRowBytes - 1;
  42.     destPtr   = destColPtr;
  43.  
  44.     /* scan src row and rotate into dest col */
  45.     while (srcrows-- > 0) {
  46.         for (i=0; i<srccols; i++) {
  47.             srcByte = *(srcPtr++);
  48.             if (srcByte & BIT07) *destPtr |= bitchar;
  49.             destPtr += destRowBytes;
  50.             if (srcByte & BIT06) *destPtr |= bitchar;
  51.             destPtr += destRowBytes;
  52.             if (srcByte & BIT05) *destPtr |= bitchar;
  53.             destPtr += destRowBytes;
  54.             if (srcByte & BIT04) *destPtr |= bitchar;
  55.             destPtr += destRowBytes;
  56.             if (srcByte & BIT03) *destPtr |= bitchar;
  57.             destPtr += destRowBytes;
  58.             if (srcByte & BIT02) *destPtr |= bitchar;
  59.             destPtr += destRowBytes;
  60.             if (srcByte & BIT01) *destPtr |= bitchar;
  61.             destPtr += destRowBytes;
  62.             if (srcByte & BIT00) *destPtr |= bitchar;
  63.             destPtr += destRowBytes;
  64.         }
  65.         if (bitchar==BIT07) {
  66.             bitchar = BIT00;
  67.             destColPtr--;
  68.         } else {
  69.             bitchar <<= 1;
  70.         }
  71.  
  72.         destPtr = destColPtr;
  73.     }
  74.     
  75.     /* remove src bitmap (portbits for offscreen port) */
  76.     DisposePtr((Ptr)macBitMap->baseAddr);
  77.     DisposePtr((Ptr)macBitMap);
  78.     
  79.     /* store new src in object */
  80.     macBitMap   = (BitMap *)newBitmap;
  81.     macBitMap->baseAddr = (Ptr) myBaseAddr;
  82.     macBitMap->rowBytes = destRowBytes;
  83.     macBitMap->bounds   = destRect;
  84. }
  85.  
  86. void main()
  87. {
  88.     WindowPtr        mainWinPtr;
  89.     OSErr            error;
  90.     SysEnvRec        theWorld;
  91.     Rect            windRect, ovalRect, myBounds;
  92.     short            myRowBytes;
  93.     
  94.     /* Make sure ColorQD exists. */
  95.     error = SysEnvirons(1, &theWorld);
  96.     if (theWorld.hasColorQD == false) {
  97.         SysBeep(50);
  98.         ExitToShell();
  99.     }
  100.     
  101.     /* Initialize all the needed managers. */
  102.     InitGraf(&qd.thePort);
  103.     InitFonts();
  104.     InitWindows();
  105.     InitMenus();
  106.     TEInit();
  107.     InitDialogs(nil);
  108.     InitCursor();
  109.  
  110.     /* Define output window with an inset clip region. */
  111.     SetRect(&windRect, 100, 100, 404, 404);
  112.     mainWinPtr = NewWindow(nil, &windRect, "\pJohn", true, documentProc, (WindowPtr) -1, false, 0);
  113.     SetPort(mainWinPtr);
  114.  
  115.     SetRect(&windRect, 0, 0, 304, 304);
  116.     EraseRect(&windRect);
  117.     SetRect(&ovalRect, 15, 15, 60, 120);
  118.     PaintOval(&ovalRect);
  119.     MoveTo(10,140);
  120.     DrawString("\pHi there.");
  121.     MoveTo(0,0);
  122.     LineTo(303,303);
  123.     MoveTo(0,303);
  124.     LineTo(303,0);
  125.  
  126.     /* rotate boundary rectangle */
  127.     myBounds = windRect;
  128.     myRowBytes = (myBounds.right - myBounds.left + 7) >> 3;
  129.     macBitMap = (BitMap *) NewPtrClear(sizeof(BitMap));
  130.     macBitMap->baseAddr = (Ptr) NewPtrClear(myRowBytes * (myBounds.bottom - myBounds.top));
  131.     macBitMap->bounds = myBounds;
  132.     macBitMap->rowBytes = myRowBytes;
  133.  
  134.     CopyBits(&(*mainWinPtr).portBits, macBitMap, &windRect, &macBitMap->bounds, 0, nil);
  135.  
  136.     /* Wait until user clicks button. */
  137.     do {
  138.     } while (!Button());
  139.  
  140.     /* Wait until user clicks button. */
  141.     do {
  142.     rotate();
  143.     CopyBits(macBitMap, &(*mainWinPtr).portBits, &macBitMap->bounds, &windRect, 0, nil);
  144.     } while (!Button());
  145. }
  146.  
  147.